<?php

App::uses('CrudController', 'Controller');
App::uses('Areas', 'Model');
App::uses('Test', 'Model');

class TestsController extends CrudController {

    public $_model = 'Test';

    public function admin($areaId = null, $moeId = null) {
        $this->request->allowMethod('get');
        $this->set('types', Test::getTypes());
        $this->set('title', __('Cursos'));
        $this->set('description', __('Creación de cursos'));
    }

    /**
     * Esta funcion se usa para obtener los examenes que puede
     *  programar el usuario logeado. Se manda llamar por Ajax
     *  desde la accion TestScheduler/admin.
     */
    public function getTestsForScheduler() {
        $m = $this->_model;
        $result = $this->$m->getForScheduler();
        $this->set(array('records' => $result, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function getTestsByArea($all = null) {
        $m = $this->_model;
        $areaId = $this->request->query['filter']['filters'][0]['value'];
        $result = $this->$m->getEnabledByArea($areaId);
        if ($all != null) {
            array_unshift($result, array("text" => 'Todas', "value" => '0'));
        }
        $this->set(array('records' => $result, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    protected function getRecords($param)
    {
        $m = $this->_model;
        $records = $this->$m->getEnabledByArea();
        return $records;
    }

    protected function c($model)
    {
        return array(
            'name' => $model->name,
            'attempts' => (integer)$model->attempts,
            'type' => (integer)$model->type,
            'min_capacity' => (integer)$model->min_capacity,
            'lock_days' => (int)$model->lock_days,
            'minimum_qualification' => (float)$model->minimum_qualification
        );
    }

    protected function u($model)
    {
        return array(
            'name' => $model->name,
            'attempts' => (integer)$model->attempts,
            'type' => (integer)$model->type,
            'min_capacity' => (integer)$model->min_capacity,
            'lock_days' => (int)$model->lock_days,
            'minimum_qualification' => (float)$model->minimum_qualification
        );
    }
}
